<?php
/**
 * 常用工具类
 */
class t {
	//自动检测字串编码并转为utf8
	static function encode($string, $outEncoding = 'UTF-8') {
		$encoding = "UTF-8";
		for ($i = 0; $i < strlen($string); $i++) {
			if (ord($string{$i}) < 128) {
				continue;
			}

			if ((ord($string{$i}) & 224) == 224) {
				//第一个字节判断通过
				$char = $string{++$i};
				if ((ord($char) & 128) == 128) {
					//第二个字节判断通过
					$char = $string{++$i};
					if ((ord($char) & 128) == 128) {
						$encoding = "UTF-8";
						break;
					}
				}
			}
			if ((ord($string{$i}) & 192) == 192) {
				//第一个字节判断通过
				$char = $string{++$i};
				if ((ord($char) & 128) == 128) {
					//第二个字节判断通过
					$encoding = "GB2312";
					break;
				}
			}
		}
		if (strtoupper($encoding) == strtoupper($outEncoding)) {
			return $string;
		} else {
			return iconv($encoding, $outEncoding, $string);
		}
	}

	/*
	 * 计算星座的函数 string get_zodiac_sign(string month, string day)
	 * 输入：月份，日期
	 * 输出：星座名称或者错误信息
	 */

	static function get_zodiac_sign($month, $day) {
		// 检查参数有效性
		if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
			return (false);
		// 星座名称以及开始日期
		$signs = array( array("20" => "宝瓶座"), array("19" => "双鱼座"), array("21" => "白羊座"), array("20" => "金牛座"), array("21" => "双子座"), array("22" => "巨蟹座"), array("23" => "狮子座"), array("23" => "处女座"), array("23" => "天秤座"), array("24" => "天蝎座"), array("22" => "射手座"), array("22" => "摩羯座"));
		list($sign_start, $sign_name) = each($signs[(int)$month - 1]);
		if ($day < $sign_start)
			list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]);
		return $sign_name;
	}

	/**
	 * 获取访客ip
	 */
	static function get_ip() {
		$ip = false;
		if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
			if ($ip) {
				array_unshift($ips, $ip);
				$ip = FALSE;
			}
			for ($i = 0; $i < count($ips); $i++) {
				if (!preg_match("/^(10|172.16|192.168)./", $ips[$i])) {
					$ip = $ips[$i];
					break;
				}
			}
		}
		return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
	}

	static function get($k, $v = null) {
		return isset($_GET[$k]) ? $_GET[$k] : $v;
	}

	static function post($k, $v = null) {
		return isset($_POST[$k]) ? $_POST[$k] : $v;
	}
	
	/*
	功能：用来过滤字符串和字符串数组，防止被挂马和sql注入
	参数$data，待过滤的字符串或字符串数组，
	$force为true，忽略get_magic_quotes_gpc
	*/
	static function in($data,$force=false){
		if(is_string($data)){
			$data=trim(htmlspecialchars($data));	//防止被挂马，跨站攻击
			if(($force==true)||(!get_magic_quotes_gpc())) {
			   $data = addslashes($data);			//防止sql注入
			}
			return  $data;
		} else if(is_array($data)) {
			foreach($data as $key=>$value){
				 $data[$key]=in($value,$force);
			}
			return $data;
		} else {
			return $data;
		}	
	}
	
	//用来还原字符串和字符串数组，把已经转义的字符还原回来
	static function out($data){
		if(is_string($data)){
			return $data = stripslashes($data);
		} else if(is_array($data)){
			foreach($data as $key=>$value){
				 $data[$key]=out($value);
			}
			return $data;
		} else {
			return $data;
		}	
	}

}
